import os
import argparse
import subprocess
from collections import deque

def parse_opts():
    parser = argparse.ArgumentParser(description='Metrics evaluation')

    parser.add_argument('--base_path', type=str, help='The FULL path of the base directory')
    parser.add_argument('--eval_path', type=str, help='The path where network predictions are saved')

    args = parser.parse_args()
    return args

def eval_all_database(args):
    path1 = args.base_path
    path2 = args.eval_path

    cmd_create_qsub = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    echo "RESPATH=$RESPATH"
    echo "ANNOT_BASE_PATH=$ANNOT_BASE_PATH"
    echo "PREDICTIONS_PATH=$PREDICTIONS_PATH"
    matlab -r "addpath(genpath('./eval_code')); create_qsub_avsal('$RESPATH', '$ANNOT_BASE_PATH', '$PREDICTIONS_PATH');"
    """.format(path1, path2)

    cmd_eval1 = r"sh eval_code/AVAD_eval.sh"
    cmd_eval2 = r"sh eval_code/DIEM_eval.sh"
    cmd_eval3 = r"sh eval_code/Coutrot1_eval.sh"
    cmd_eval4 = r"sh eval_code/Coutrot2_eval.sh"
    cmd_eval5 = r"sh eval_code/ETMD_eval.sh"
    cmd_eval6 = r"sh eval_code/SumMe_eval.sh"
    cmd_deque = deque([cmd_eval4, cmd_eval5, cmd_eval6])

    cmd_compute1 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_diem('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)
    cmd_compute2 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_coutrot1('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)
    cmd_compute3 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_coutrot2('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)
    cmd_compute4 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_summe('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)
    cmd_compute5 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_etmd('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)
    cmd_compute6 = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"
    ANNOT_BASE_PATH=$path1"/data"
    PREDICTIONS_PATH=$path1"/"$path2

    matlab -r "addpath(genpath('./eval_code')); compute_all_avad('$RESPATH', '$ANNOT_BASE_PATH');"
    """.format(path1, path2)

    cmd_view_results = r"""
    path1={}
    path2={}
    RESPATH=$path1"/"$path2"/results_per_video"

    cat $RESPATH'/'final_results_DIEM.txt
    cat $RESPATH'/'final_results_AVAD.txt
    cat $RESPATH'/'final_results_Coutrot1.txt
    cat $RESPATH'/'final_results_Coutrot2.txt
    cat $RESPATH'/'final_results_SumMe.txt
    cat $RESPATH'/'final_results_ETMD.txt
    """.format(path1, path2)

    status = subprocess.run(cmd_create_qsub, shell=True, check=True)

    # thread1 = subprocess.Popen(cmd_eval1, shell=True)
    # thread2 = subprocess.Popen(cmd_eval2, shell=True)
    # thread3 = subprocess.Popen(cmd_eval3, shell=True)
    threads = [subprocess.Popen(cmd, shell=True) for cmd in [cmd_eval1, cmd_eval2, cmd_eval3]]
    eval_complete = False
    while(len(threads) > 0):
        for i, thread in enumerate(threads):
            if thread.poll() is not None:
                if len(cmd_deque) > 0:
                    cmd = cmd_deque.pop()
                    threads.append(subprocess.Popen(cmd, shell=True))
                del threads[i]

    # sub_eval_4 = subprocess.Popen(cmd_eval4, shell=True)
    # sub_eval_5 = subprocess.Popen(cmd_eval5, shell=True)
    # sub_eval_6 = subprocess.Popen(cmd_eval6, shell=True)
    # eval_complete = False
    # while(eval_complete is not True):
    #     eval_wait4 = sub_eval_4.poll()
    #     eval_wait5 = sub_eval_5.poll()
    #     eval_wait6 = sub_eval_6.poll()
    #     if eval_wait4 is not None and eval_wait5 is not None and eval_wait6 is not None:
    #         eval_complete = True

    sub_eval_1 = subprocess.Popen(cmd_compute1, shell=True)
    sub_eval_2 = subprocess.Popen(cmd_compute2, shell=True)
    sub_eval_3 = subprocess.Popen(cmd_compute3, shell=True)
    eval_complete = False
    while(eval_complete is not True):
        eval_wait1 = sub_eval_1.poll()
        eval_wait2 = sub_eval_2.poll()
        eval_wait3 = sub_eval_3.poll()
        if eval_wait1 is not None and eval_wait2 is not None and eval_wait3 is not None:
            eval_complete = True

    sub_eval_4 = subprocess.Popen(cmd_compute4, shell=True)
    sub_eval_5 = subprocess.Popen(cmd_compute5, shell=True)
    sub_eval_6 = subprocess.Popen(cmd_compute6, shell=True)
    eval_complete = False
    while(eval_complete is not True):
        eval_wait4 = sub_eval_4.poll()
        eval_wait5 = sub_eval_5.poll()
        eval_wait6 = sub_eval_6.poll()
        if eval_wait4 is not None and eval_wait5 is not None and eval_wait6 is not None:
            eval_complete = True

    status = subprocess.run(cmd_view_results, shell=True, check=True)
    return 1
if __name__ == '__main__':
    args = parse_opts()
    eval_all_database(args)
